package com.ekuaitu.kuaitu.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Build;
import android.os.CountDownTimer;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.model.LatLng;
import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.api.ApiStore;
import com.ekuaitu.kuaitu.api.UrlConstant;
import com.ekuaitu.kuaitu.base.BaseActivity;
import com.ekuaitu.kuaitu.bean.CancelOrder;
import com.ekuaitu.kuaitu.bean.HeartBean;
import com.ekuaitu.kuaitu.bean.InputSNSuccEvent;
import com.ekuaitu.kuaitu.bean.OpenBikeBean;
import com.ekuaitu.kuaitu.bean.OperateBean;
import com.ekuaitu.kuaitu.bean.QueryLockBean;
import com.ekuaitu.kuaitu.bean.ScanSussEvent;
import com.ekuaitu.kuaitu.bean.ValidateBikeBean;
import com.ekuaitu.kuaitu.ble.BluetoothHelper;
import com.ekuaitu.kuaitu.ble.ClientManager;
import com.ekuaitu.kuaitu.utils.CheckRegexUtils;
import com.ekuaitu.kuaitu.utils.Constants;
import com.ekuaitu.kuaitu.utils.IntentKey;
import com.ekuaitu.kuaitu.utils.KuaituDialog;
import com.ekuaitu.kuaitu.utils.MyApplication;
import com.ekuaitu.kuaitu.utils.NetUtils;
import com.ekuaitu.kuaitu.utils.StatusBarUtil;
import com.ekuaitu.kuaitu.utils.Toast.ToastCompat;
import com.inuker.bluetooth.library.connect.listener.BluetoothStateListener;
import com.inuker.bluetooth.library.search.SearchResult;
import com.inuker.bluetooth.library.search.response.SearchResponse;
import com.jungly.gridpasswordview.GridPasswordView;
import com.umeng.analytics.MobclickAgent;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class InputSNActivity extends BaseActivity implements AMapLocationListener {

    @BindView(R.id.iv_return_input_use_sn)
    ImageView ivReturnInputUseSn;
    @BindView(R.id.grid_input_use_sn)
    GridPasswordView gridInputUseSn;
    @BindView(R.id.tv_input_use_ok)
    TextView tvInputUseOk;
    @BindView(R.id.rel_input_use_input)
    AutoRelativeLayout relInputUseInput;
    @BindView(R.id.lin_input_use_scan)
    AutoLinearLayout linInputUseScan;
    @BindView(R.id.lin_input_use_input)
    AutoLinearLayout linInputUseInput;
    @BindView(R.id.iv_input_use_light)
    ImageView ivInputUseLight;
    @BindView(R.id.tv_input_use_light)
    TextView tvInputUseLight;
    @BindView(R.id.lin_input_use_light)
    AutoLinearLayout linInputUseLight;
    @BindView(R.id.progressBar_input)
    AutoLinearLayout progressBar_input;
    @BindView(R.id.activity_input_use_sn)
    AutoRelativeLayout activityInputUseSn;
    private Context mContext = this;
    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationOption;
    private AMapLocation aMapLocation;
    private Intent intent;
    private String bikeId;
    private String orderId;
    private int lockType;
    private String bikeLicenseDesignated;
    private LatLng curPosition;
    private boolean isLightOn = false;
    private int queryCount = 0;
    private CountDownTimer timerQuery;
    private int requestCode;
    private Camera.Parameters parameters;
    private int isRedBike = 0;
    private Camera camera;
    private int power;
    private int caozuo = 0;

    private byte[] loadPwd = new byte[6];
    private byte[] loadKey = new byte[16];
    private String btMac = "";
    private boolean isOpenLock = false;
    private int openType = -1;
    /* byte[] loadKey = {32, 87, 47, 82, 54, 75, 63, 71, 48, 80, 65, 88, 17, 99, 45, 43};
     byte[] loadPwd = {0x30, 0x30, 0x30, 0x30, 0x30, 0x30};*/
    private String scanedBikeLicense = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_input_sn;
    }


    @Override
    public void initData() {
        intent = getIntent();
        orderId = intent.getStringExtra(IntentKey.INTENT_ORDER_ID);
        bikeId = intent.getStringExtra(IntentKey.INTENT_BIKE_ID);
        lockType = intent.getIntExtra(IntentKey.INTENT_LOCK_TYPE, -1);
        isRedBike = intent.getIntExtra("isRedBike", 0);
        bikeLicenseDesignated = intent.getStringExtra(IntentKey.INTENT_BIKE_LICENSE_DESIGNATED);
        requestCode = getIntent().getIntExtra("requestCode", 0);
        initView();
        getPermissions();
    }

    public void initView() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.black_pure), 0, false);
        tvInputUseOk.setClickable(false);
        gridInputUseSn.setPasswordVisibility(true);
        gridInputUseSn.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(String psw) {
                tvInputUseOk.setClickable(false);
                tvInputUseOk.setBackgroundColor(Color.parseColor("#c8c8c8"));
            }

            @Override
            public void onInputFinish(String psw) {
                tvInputUseOk.setClickable(true);
                tvInputUseOk.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            }
        });
    }

    private void startLocation() {
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        mLocationOption = new AMapLocationClientOption();
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setOnceLocation(true);
        mLocationClient.setLocationOption(mLocationOption);
        mLocationClient.startLocation();
    }

    private void getPermissions() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                        Constants.READ_LOCATION_PERMISSION_REQUEST);
            } else {
                startLocation();
            }
        } else {
            startLocation();
        }
    }

    @OnClick({R.id.iv_return_input_use_sn, R.id.tv_input_use_ok, R.id.lin_input_use_scan, R.id.lin_input_use_light, R.id.activity_input_use_sn, R.id.iv_input_question})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_return_input_use_sn:
                finish();
                break;
            case R.id.tv_input_use_ok:
                MobclickAgent.onEvent(this, Constants.MANUAL_OPEN);
                handleResult(UrlConstant.Request.QR_URL + gridInputUseSn.getPassWord());
                break;
            case R.id.lin_input_use_scan:
                MobclickAgent.onEvent(this, Constants.SCANCODE_QR_BIKE);
                finish();
                break;
            case R.id.lin_input_use_light:
                MobclickAgent.onEvent(this, Constants.SCANCODE_FLASHLIGHT_BIKE);
                if (!isLightOn) {
                    try {
                        camera = Camera.open();
                        camera.setPreviewTexture(new SurfaceTexture(0));
                        camera.startPreview();
                        parameters = camera.getParameters();
                        parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);// 开启
                        camera.setParameters(parameters);
                    } catch (IOException | RuntimeException e) {
                        e.printStackTrace();
                    }

                    ivInputUseLight.setImageDrawable(getResources().getDrawable(R.drawable.light_on));
                    tvInputUseLight.setTextColor(getResources().getColor(R.color.colorPrimary));
                    isLightOn = true;
                } else {
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);// 关闭
                    camera.setParameters(parameters);
                    camera.stopPreview();
                    camera.release();
                    ivInputUseLight.setImageDrawable(getResources().getDrawable(R.drawable.light_off));
                    tvInputUseLight.setTextColor(getResources().getColor(R.color.white));
                    isLightOn = false;
                }
            case R.id.activity_input_use_sn:
                break;
            case R.id.iv_input_question:
                Intent intent = new Intent();
                intent.setClass(mContext, WebViewActivity.class);
                intent.putExtra(IntentKey.INTENT_WEBVIEW_URL, UrlConstant.Request.SCAN_HELP);
                intent.putExtra(IntentKey.INTENT_WEBVIEW_TITLE, "帮助");
                startActivity(intent);
                break;
        }
    }


    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {
            this.aMapLocation = aMapLocation;
            curPosition = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());
        } else {
            ToastCompat.makeText(mContext, "正在获取位置信息", Toast.LENGTH_SHORT).setGravity(Gravity.CENTER, 0, 0).show();
            mLocationClient.startLocation();
        }
    }


    private void handleResult(String result) {
        isOpenLock = true;
        if (curPosition == null) {
            ToastCompat.makeText(mContext, "未能获取位置信息", Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
        } else {
            if (result.length() == 52 && result.substring(0, 44).equals(UrlConstant.Request.QR_URL) && CheckRegexUtils.checkQRCode(result.substring(44, 52))) {  //检验二维码是否是快兔的二维码
                scanedBikeLicense = result.substring(44, 52);
                if (bikeLicenseDesignated == null) {
                    Log.i("params empty", "走了正常的开锁");//直接扫码，不区分是否任务车
                    validationBike(scanedBikeLicense);
                } else if (isRedBike == 1) {
                    Log.i("params empty", "走了任务车开锁");//点任务车接受任务（地图点击任务车图标，接受任务）
                    isDesignatedRebBike(scanedBikeLicense);
                } else {
                    Log.i("params empty", "走了骑行中开锁");

                    //骑行中开锁
                    isInUsedBike(scanedBikeLicense);
                }
            } else {
                ToastCompat.makeText(mContext, "请扫描快兔单车的二维码", Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
            }
        }
    }

    //判断要开锁的是不是已经选择的任务单车
    private void isDesignatedRebBike(String scanedBikeLicense) {
        if (this.bikeLicenseDesignated.equals(scanedBikeLicense)) {
            validationRedBike(scanedBikeLicense);
        } else {
            ToastCompat.makeText(mContext, "请输入任务中的单车码", Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
        }
    }


    //骑行中判断要开锁的是不是再租用的车
    private void isInUsedBike(final String bikeLicense) {
        if (this.bikeLicenseDesignated.equals(bikeLicense)) {
            if (lockType == 1) {
                //骑行中开锁
                openGpsBike(true);
            } else if (lockType == 2 || lockType == 3 || lockType == 4) {
                requestBTBikeInfo(bikeId);
            }
        } else {//租用的不是这辆单车
            if (requestCode == Constants.REQUEST_SCAN_INUSE) {
                ToastCompat.makeText(mContext, "您租用的不是这辆单车", Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
            } else {
                validationBikeForNew(this.bikeLicenseDesignated, bikeLicense);
            }
        }
    }

    /**
     * @param isInUse 单车是否使用中GPS开锁
     */
    private void openGpsBike(boolean isInUse) {
        commitBikeLogPre();
        if (isInUse) {
            progressBar_input.setVisibility(View.VISIBLE);
            ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).openBikeInUse(MyApplication.getInstance().getRequestFieldMap(), orderId, bikeId, curPosition.longitude + "", curPosition.latitude + "", "1", "1").enqueue(new Callback<OperateBean>() {
                @Override
                public void onResponse(Call<OperateBean> call, Response<OperateBean> response) {
                    if (response.isSuccessful() && response.body() != null) {
                        if (response.body().getStatus() == 200) {
                            progressBar_input.setVisibility(View.VISIBLE);
                            sendHeat();
                            queryCount = 0;
                            queryLockInfo();
                            commitBikeLogSuccess();
                        } else {
                            progressBar_input.setVisibility(View.GONE);

                            ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                            commitBikeLogFailed(response.body().getStatus() + "", response.body().getMessage());
                        }
                    } else {
                        progressBar_input.setVisibility(View.GONE);
                        ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                    }
                }

                @Override
                public void onFailure(Call<OperateBean> call, Throwable t) {
                    progressBar_input.setVisibility(View.GONE);
                    ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                }
            });
        } else {
            progressBar_input.setVisibility(View.VISIBLE);
            ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).openBike(MyApplication.getInstance().getRequestFieldMap(), bikeId, "1", curPosition.latitude + "", curPosition.longitude + "").enqueue(new Callback<OpenBikeBean>() {
                @Override
                public void onResponse(Call<OpenBikeBean> call, Response<OpenBikeBean> response) {
                    if (response.isSuccessful() && response.body() != null) {
                        if (response.body().getStatus() == 200) {
                            progressBar_input.setVisibility(View.VISIBLE);
                            orderId = response.body().getAttachment().getOrderModel().getId();
                            sendHeat();
                            queryCount = 0;
                            queryLockInfo();
                            commitBikeLogSuccess();
                        } else {

                            progressBar_input.setVisibility(View.GONE);
                            ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                            commitBikeLogFailed(response.body().getStatus() + "", response.body().getMessage());
                        }
                    } else {
                        progressBar_input.setVisibility(View.GONE);
                        ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                    }
                }

                @Override
                public void onFailure(Call<OpenBikeBean> call, Throwable t) {
                    progressBar_input.setVisibility(View.GONE);
                    ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                }
            });
        }
    }

    private void queryLockInfo() {
        if (queryCount < 3 && queryCount >= 0) {
            String isSelect;
            if (queryCount == 1) {
                isSelect = "1";
            } else {
                isSelect = "0";
            }
            progressBar_input.setVisibility(View.VISIBLE);
            ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).queryLockInfo(MyApplication.getInstance().getRequestFieldMap(), bikeId, isSelect).enqueue(new Callback<QueryLockBean>() {
                @Override
                public void onResponse(Call<QueryLockBean> call, Response<QueryLockBean> response) {
                    if (response.isSuccessful() && response.body() != null) {
                        if (response.body().getStatus() == 200) {
                            progressBar_input.setVisibility(View.GONE);
                            if (response.body().getAttachment().getLockstatus() == 1) {
                                queryCount = -1;
                                Intent intent = new Intent();
                                intent.putExtra(IntentKey.INTENT_ORDER_ID, orderId);
                                EventBus.getDefault().post(new InputSNSuccEvent(true));
                                if (requestCode == Constants.REQUEST_SCAN_UNPRE) {//直接扫码开锁成功后，任务跳转任务骑行中，普通跳转普通骑行中
                                    if (isRedBike == 0) {
                                        intent.setClass(mContext, BikeInUseRedActivity.class);
                                    } else {
                                        intent.setClass(mContext, BikeInUseRedActivity.class);
                                    }
                                    startActivity(intent);
                                    finish();
                                } else if (requestCode == Constants.REQUEST_SCAN_INUSE) {
                                    finish();
                                } else if (requestCode == Constants.REQUEST_SCAN_RED) {
                                    intent.setClass(mContext, BikeInUseRedActivity.class);
                                    EventBus.getDefault().post(new ScanSussEvent(true));
                                    startActivity(intent);
                                    finish();
                                }
                                if (isLightOn) {
                                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);// 关闭
                                    camera.setParameters(parameters);
                                    camera.stopPreview();
                                    camera.release();
                                    ivInputUseLight.setImageDrawable(getResources().getDrawable(R.drawable.light_off));
                                    tvInputUseLight.setTextColor(getResources().getColor(R.color.white));
                                    isLightOn = false;
                                }
                            } else {
                                progressBar_input.setVisibility(View.VISIBLE);
                                queryCount++;
                                if (timerQuery != null) {
                                    timerQuery.cancel();
                                    timerQuery = null;
                                }
                                timerQuery = new CountDownTimer(5000, 1000) {
                                    @Override
                                    public void onTick(long l) {
                                        progressBar_input.setVisibility(View.VISIBLE);
                                    }

                                    @Override
                                    public void onFinish() {
                                        queryLockInfo();
                                    }
                                };
                                timerQuery.start();
                            }
                        } else {
                            ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                        }
                    } else {
                        progressBar_input.setVisibility(View.GONE);
                        ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                    }
                }

                @Override
                public void onFailure(Call<QueryLockBean> call, Throwable t) {
                    progressBar_input.setVisibility(View.GONE);
                    ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                }
            });
        } else {
            progressBar_input.setVisibility(View.GONE);
            final KuaituDialog kuaituDialog = new KuaituDialog(InputSNActivity.this);
            kuaituDialog.setTitle(getString(R.string.dialogTitle));
            kuaituDialog.setMessage("网络不稳定，重新尝试打开车锁");
            kuaituDialog.setYesOnclickListener("确定", new KuaituDialog.onYesOnclickListener() {
                @Override
                public void onYesClick() {
                    queryCount = 0;
                    queryLockInfo();
                    kuaituDialog.dismiss();
                }
            });
            kuaituDialog.setNoOnclickListener("取消", new KuaituDialog.onNoOnclickListener() {
                @Override
                public void onNoClick() {
                    kuaituDialog.dismiss();
                }
            });
            kuaituDialog.setCancelable(false);
            kuaituDialog.show();
        }
    }

    private void validationBike(final String scanedBikeLicense) {
        progressBar_input.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).validateBike(MyApplication.getInstance().getRequestFieldMap(), scanedBikeLicense, curPosition.latitude + "", curPosition.longitude + "")
                .enqueue(new Callback<ValidateBikeBean>() {
                    @Override
                    public void onResponse(Call<ValidateBikeBean> call, Response<ValidateBikeBean> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            if (response.body().getStatus() == 200) {
                                lockType = response.body().getAttachment().getBikeInfo().getLockType();
                                bikeId = response.body().getAttachment().getBikeInfo().getBikeId();
                                Log.i("params empty", "验证了单车bikeId=" + bikeId);
                                //if (response.body().getAttachment().getBikeInfo().getIsRedPackage() == 0) {          //直接扫码，非任务车继续开锁流程
                                if (lockType == 1) {
                                    openGpsBike(false);
                                } else if (lockType == 2 || lockType == 3 || lockType == 4) {
                                    requestBTBikeInfo(bikeId);
                                }
                                //} else if (response.body().getAttachment().getBikeInfo().getIsRedPackage() == 1) {  //直接扫码，任务车跳转任务地图
                                /*EventBus.getDefault().post(new InputSNSuccEvent(true));
                                Intent intent = new Intent(mContext, BikeRedTaskActivity.class);
                                intent.putExtra("scanedRedBikeLicense", scanedBikeLicense);
                                intent.putExtra(IntentKey.INTENT_LOCK_TYPE, lockType);
                                intent.putExtra("redBikeLatitude", response.body().getAttachment().getBikeInfo().getLatitude());
                                intent.putExtra("redBikeLongitude", response.body().getAttachment().getBikeInfo().getLongitude());
                                intent.putExtra(IntentKey.INTENT_BIKE_ID, bikeId);
                                startActivity(intent);
                                finish();*/
                                //}

                            } else {
                                progressBar_input.setVisibility(View.GONE);
                                ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                                commitBikeLogFailed(response.body().getStatus() + "", response.body().getMessage());
                            }
                        } else {
                            progressBar_input.setVisibility(View.GONE);
                            ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<ValidateBikeBean> call, Throwable t) {
                        progressBar_input.setVisibility(View.GONE);
                        ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                    }
                });
    }

    private void validationRedBike(final String scanedBikeLicense) {
        progressBar_input.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).validateBike(MyApplication.getInstance().getRequestFieldMap(), scanedBikeLicense, curPosition.latitude + "", curPosition.longitude + "")
                .enqueue(new Callback<ValidateBikeBean>() {
                    @Override
                    public void onResponse(Call<ValidateBikeBean> call, Response<ValidateBikeBean> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            if (response.body().getStatus() == 200) {
                                lockType = response.body().getAttachment().getBikeInfo().getLockType();
                                bikeId = response.body().getAttachment().getBikeInfo().getBikeId();
                                if (lockType == 1) {
                                    openGpsBike(false);
                                } else if (lockType == 2 || lockType == 3 || lockType == 4) {
                                    requestBTBikeInfo(bikeId);
                                }

                            } else {
                                progressBar_input.setVisibility(View.GONE);
                                ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                                commitBikeLogFailed(response.body().getStatus() + "", response.body().getMessage());
                            }
                        } else {
                            progressBar_input.setVisibility(View.GONE);
                            ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<ValidateBikeBean> call, Throwable t) {
                        progressBar_input.setVisibility(View.GONE);
                        ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                    }
                });
    }

    private void validationBikeForNew(final String oldBikeLicense, final String newBikeLicense) {
        progressBar_input.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).validateBike(MyApplication.getInstance().getRequestFieldMap(), newBikeLicense, curPosition.latitude + "", curPosition.longitude + "")
                .enqueue(new Callback<ValidateBikeBean>() {
                    @Override
                    public void onResponse(Call<ValidateBikeBean> call, final Response<ValidateBikeBean> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            if (response.body().getStatus() == 200) {
                                if (response.body().getAttachment().getBikeInfo().getLockType() == 1) {
                                    progressBar_input.setVisibility(View.VISIBLE);
                                    final KuaituDialog kuaituDialog = new KuaituDialog(mContext);
                                    kuaituDialog.setTitle(getString(R.string.dialogTitle));
                                    kuaituDialog.setMessage("您扫描的不是预约单车,是否直接租用这辆单车？");
                                    kuaituDialog.setYesOnclickListener("我要租用", new KuaituDialog.onYesOnclickListener() {
                                        @Override
                                        public void onYesClick() {
                                            kuaituDialog.dismiss();
                                            cancelBikeOrderForNew(oldBikeLicense, newBikeLicense, response.body().getAttachment().getBikeInfo().getBikeId(), 1);
                                        }
                                    });
                                    kuaituDialog.setNoOnclickListener("我不租用", new KuaituDialog.onNoOnclickListener() {
                                        @Override
                                        public void onNoClick() {
                                            kuaituDialog.dismiss();
                                            progressBar_input.setVisibility(View.GONE);
                                        }
                                    });
                                    kuaituDialog.show();
                                } else if (response.body().getAttachment().getBikeInfo().getLockType() == 2 ||
                                        response.body().getAttachment().getBikeInfo().getLockType() == 3 ||
                                        response.body().getAttachment().getBikeInfo().getLockType() == 4) {
                                    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
                                        final KuaituDialog kuaituDialog = new KuaituDialog(mContext);
                                        kuaituDialog.setTitle(getString(R.string.dialogTitle));
                                        kuaituDialog.setMessage("对不起！您的手机不支持蓝牙4.0，请租用快兔出行的非蓝牙单车。");
                                        kuaituDialog.setYesOnclickListener("确认", new KuaituDialog.onYesOnclickListener() {
                                            @Override
                                            public void onYesClick() {
                                                progressBar_input.setVisibility(View.GONE);
                                                kuaituDialog.dismiss();
                                            }
                                        });
                                        kuaituDialog.setNoOnclickListener("取消", new KuaituDialog.onNoOnclickListener() {
                                            @Override
                                            public void onNoClick() {
                                                progressBar_input.setVisibility(View.GONE);
                                                kuaituDialog.dismiss();
                                            }
                                        });
                                        kuaituDialog.show();
                                    } else {
                                        final KuaituDialog kuaituDialog = new KuaituDialog(mContext);
                                        kuaituDialog.setTitle(getString(R.string.dialogTitle));
                                        kuaituDialog.setMessage("您扫描的不是预约单车,是否直接租用这辆单车？");
                                        kuaituDialog.setYesOnclickListener("我要租用", new KuaituDialog.onYesOnclickListener() {
                                            @Override
                                            public void onYesClick() {
                                                kuaituDialog.dismiss();
                                                cancelBikeOrderForNew(oldBikeLicense, newBikeLicense, response.body().getAttachment().getBikeInfo().getBikeId(), 2);
                                            }
                                        });
                                        kuaituDialog.setNoOnclickListener("我不租用", new KuaituDialog.onNoOnclickListener() {
                                            @Override
                                            public void onNoClick() {
                                                progressBar_input.setVisibility(View.GONE);
                                                kuaituDialog.dismiss();
                                            }
                                        });
                                        kuaituDialog.show();
                                    }
                                }
                            } else {
                                progressBar_input.setVisibility(View.GONE);
                                ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                            }
                        } else {
                            progressBar_input.setVisibility(View.GONE);
                            ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<ValidateBikeBean> call, Throwable t) {
                        progressBar_input.setVisibility(View.GONE);
                        ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                    }
                });
    }

    private void cancelBikeOrderForNew(final String newBikeLicense, final String oldBikeLicense, final String newBikeId, final int lockType) {
        progressBar_input.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).cancelBikeOrder(MyApplication.getInstance().getRequestFieldMap(), orderId)
                .enqueue(new Callback<CancelOrder>() {
                    @Override
                    public void onResponse(Call<CancelOrder> call, Response<CancelOrder> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            progressBar_input.setVisibility(View.GONE);
                            InputSNActivity.this.lockType = lockType;
                            if (response.body().getStatus() == 200) {
                                bikeLicenseDesignated = null;
                                if (lockType == 1) {
                                    EventBus.getDefault().post(new ScanSussEvent(true));
                                    bikeId = newBikeId;
                                    //无预约开锁
                                    openGpsBike(false);
                                } else if (lockType == 2 || lockType == 3 || lockType == 4) {
                                    EventBus.getDefault().post(new ScanSussEvent(true));
                                    bikeId = newBikeId;
                                    openType = 1;
                                    requestBTBikeInfo(bikeId);
                                }
                            } else if (response.body().getStatus() == 400) {
                                ToastCompat.makeText(mContext, "订单不存在", Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                            } else {
                                ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<CancelOrder> call, Throwable t) {
                        progressBar_input.setVisibility(View.GONE);
                        ToastCompat.makeText(mContext, getResources().getString(R.string.findBikeFailure), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                    }
                });
    }

    private void requestBTBikeInfo(String bikeId) {
        commitBikeLogPre();
        progressBar_input.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).requestBTBikeInfo(MyApplication.getInstance().getRequestFieldMap(), bikeId, "0")
                .enqueue(new Callback<QueryLockBean>() {
                    @Override
                    public void onResponse(Call<QueryLockBean> call, final Response<QueryLockBean> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            if (response.body().getStatus() == 200) {
                                //  progressBar_input.setVisibility(View.VISIBLE);
                                btMac = response.body().getAttachment().getBikeMachineUnique();
                                String key = response.body().getAttachment().getBikeMachineKey();
                                String pwd = response.body().getAttachment().getBikeMachinePassword();
                                String[] KEY = key.split(",");
                                String[] PWD = pwd.split(",");
                                for (int i = 0; i < 16; i++) {
                                    loadKey[i] = Byte.parseByte(KEY[i]);
                                }
                                for (int j = 0; j < 6; j++) {
                                    loadPwd[j] = Byte.parseByte(PWD[j]);
                                }

                                ClientManager.getClient().registerBluetoothStateListener(mBluetoothStateListener);
                                if (ClientManager.getClient().isBluetoothOpened()) {
                                    int status = ClientManager.getClient().getConnectStatus(btMac);
                                    Log.i("scanresultInput", "firstscan " + status + " " + btMac);
                                    if (status == 2) {
                                        caozuo = 0;//开锁
                                        BluetoothHelper.getInstance().loadLockData(btMac, MyApplication.btToken, loadPwd, loadKey);
                                        BluetoothHelper.getInstance().setBluetoothCommandCallback(commandCallback);//蓝牙指令回调
                                        BluetoothHelper.getInstance().openLock();
                                    } else {
                                        BluetoothHelper.getInstance().searchDevice(mSearchResponse, commandCallback);
                                    }

                                } else {
                                    ClientManager.getClient().openBluetooth();
                                }
                            } else {
                                progressBar_input.setVisibility(View.GONE);
                                ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                            }
                        } else {
                            progressBar_input.setVisibility(View.GONE);
                            ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<QueryLockBean> call, Throwable t) {
                        progressBar_input.setVisibility(View.GONE);
                        ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                    }
                });

    }

    //蓝牙打开结果
    private final BluetoothStateListener mBluetoothStateListener = new BluetoothStateListener() {
        @Override
        public void onBluetoothStateChanged(boolean openOrClosed) {
            if (isOpenLock) {
                isOpenLock = false;
                if (openOrClosed) {
                    BluetoothHelper.getInstance().searchDevice(mSearchResponse, commandCallback);
                } else {
                    progressBar_input.setVisibility(View.GONE);
                    ToastCompat.makeText(mContext, "蓝牙未开启", Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                }
            }
        }
    };

    //蓝牙设备搜索结果
    private final SearchResponse mSearchResponse = new SearchResponse() {
        @Override
        public void onSearchStarted() {

        }

        @Override
        public void onDeviceFounded(SearchResult device) {
            if (device.scanRecord[5] == 0x01 && device.scanRecord[6] == 0x02) {
                if (device.getAddress().equals(btMac)) {
                    ClientManager.getClient().stopSearch();
                    power = device.scanRecord[14];
                    MyApplication.power = power;
                    BluetoothHelper.getInstance().connectDevice(btMac, loadPwd, loadKey, new BluetoothHelper.OnConnectListener() {
                        @Override
                        public void onConnectSuccess() {
                            ClientManager.getClient().stopSearch();
                            Log.i("scanresultInput", "连接成功" + btMac);
                            if (requestCode == Constants.REQUEST_SCAN_UNPRE || requestCode == Constants.REQUEST_SCAN_RED || openType == 1) {
                                openType = -1;
                                requestOpenBtBike();
                            } else if (requestCode == Constants.REQUEST_SCAN_INUSE) {//骑行中开蓝牙锁
                                //骑行中开蓝牙锁
                                requestOpenBtBikeInUse();
                            }
                        }

                        @Override
                        public void onConnectFailed() {
                            commitBikeLogFailed("0", "连接蓝牙失败");
                            progressBar_input.setVisibility(View.GONE);
                            final KuaituDialog kuaituDialog = new KuaituDialog(mContext);
                            kuaituDialog.setTitle(getString(R.string.dialogTitle));
                            kuaituDialog.setMessage("对不起！未能连接到这个单车，您可以再次尝试。");
                            kuaituDialog.setYesOnclickListener("确认", new KuaituDialog.onYesOnclickListener() {
                                @Override
                                public void onYesClick() {
                                    kuaituDialog.dismiss();
                                    ClientManager.getClient().closeBluetooth();
                                }
                            });
                            kuaituDialog.setNoOnclickListener("取消", new KuaituDialog.onNoOnclickListener() {
                                @Override
                                public void onNoClick() {
                                    kuaituDialog.dismiss();
                                    ClientManager.getClient().closeBluetooth();
                                }
                            });
                            kuaituDialog.show();
                        }
                    });
                }
            }
        }

        @Override
        public void onSearchStopped() {
            commitBikeLogFailed("0", "未扫描到该单车，请将手机靠近车锁再试");
            progressBar_input.setVisibility(View.GONE);
            final KuaituDialog kuaituDialog = new KuaituDialog(InputSNActivity.this);
            kuaituDialog.setTitle(getString(R.string.dialogTitle));
            kuaituDialog.setMessage("未扫描到该单车，请您将手机靠近车锁再试");
            kuaituDialog.setYesOnclickListener("确定", new KuaituDialog.onYesOnclickListener() {
                @Override
                public void onYesClick() {
                    kuaituDialog.dismiss();
                }
            });
            kuaituDialog.setNoOnclickListener("取消", new KuaituDialog.onNoOnclickListener() {
                @Override
                public void onNoClick() {
                    kuaituDialog.dismiss();
                }
            });
            kuaituDialog.setCancelable(false);
            kuaituDialog.show();
        }

        @Override
        public void onSearchCanceled() {
        }
    };

    private void requestOpenBtBikeInUse() {
        progressBar_input.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).requestOpenBtBikeInUse(MyApplication.getInstance().getRequestFieldMap(), orderId, bikeId, "", lockType + "", power + "", curPosition.longitude + "", curPosition.latitude + "")
                .enqueue(new Callback<OpenBikeBean>() {
                    @Override
                    public void onResponse(Call<OpenBikeBean> call, final Response<OpenBikeBean> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            if (response.body().getStatus() == 200) {
                                Log.i("params empty", "走了骑行中请求开锁orderId=" + orderId);
                                sendHeat();
                                caozuo = 0;
                                BluetoothHelper.getInstance().bluetoothGetToken();
                            } else {
                                Log.i("params empty", "003");
                                progressBar_input.setVisibility(View.GONE);
                                ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                            }
                        } else {
                            progressBar_input.setVisibility(View.GONE);
                            ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<OpenBikeBean> call, Throwable t) {
                        progressBar_input.setVisibility(View.GONE);
                        ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                    }
                });
    }


    //验证完单车可用后请求开锁
    private void requestOpenBtBike() {
        progressBar_input.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).requestOpenBtBike(MyApplication.getInstance().getRequestFieldMap(), bikeId, lockType + "", power + "", curPosition.longitude + "", curPosition.latitude + "")
                .enqueue(new Callback<OpenBikeBean>() {
                    @Override
                    public void onResponse(Call<OpenBikeBean> call, final Response<OpenBikeBean> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            if (response.body().getStatus() == 200) {
                                orderId = response.body().getAttachment().getOrderModel().getId();
                                Log.i("params empty", "走了验证完单车可用后请求开锁orderId=" + orderId);
                                sendHeat();
                                caozuo = 0;
                                BluetoothHelper.getInstance().bluetoothGetToken();
                            } else {
                                progressBar_input.setVisibility(View.GONE);
                                Log.i("params empty", "002");
                                ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                            }
                        } else {
                            progressBar_input.setVisibility(View.GONE);
                            ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<OpenBikeBean> call, Throwable t) {
                        progressBar_input.setVisibility(View.GONE);
                        ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                    }
                });
    }

    //开锁成功并获取电量后回传服务器锁信息
    private void sendBTLockInfo(final int lockStatus, final int operationStatus) {
        final int operationType;
        if (requestCode == Constants.REQUEST_SCAN_RED || requestCode == Constants.REQUEST_SCAN_UNPRE) {
            operationType = 1;
        } else {
            operationType = 2;
        }
        progressBar_input.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).sendBTLockInfo(MyApplication.getInstance().getRequestFieldMap(), bikeId, lockType + "", power + "",
                curPosition.longitude + "", curPosition.latitude + "", lockStatus + "", operationStatus + "", orderId, operationType + "")
                .enqueue(new Callback<OperateBean>() {
                    @Override
                    public void onResponse(Call<OperateBean> call, final Response<OperateBean> response) {
                        if (response.isSuccessful() && response.body() != null) {
                            progressBar_input.setVisibility(View.GONE);
                            if (response.body().getStatus() == 200) {
                                if (lockStatus == 1) {
                                    Intent intent = new Intent();
                                    intent.putExtra(IntentKey.INTENT_ORDER_ID, orderId);
                                    EventBus.getDefault().post(new InputSNSuccEvent(true));  //finish掉扫码页面
                                    if (requestCode == Constants.REQUEST_SCAN_RED) {
                                        intent.setClass(mContext, BikeInUseRedActivity.class);
                                        EventBus.getDefault().post(new ScanSussEvent(true));
                                        startActivity(intent);
                                        finish();
                                    } else if (requestCode == Constants.REQUEST_SCAN_UNPRE) {//直接扫码开锁成功后，任务跳转任务骑行中，普通跳转普通骑行中
                                        if (isRedBike == 0) {
                                            intent.setClass(mContext, BikeInUseRedActivity.class);
                                        } else {
                                            intent.setClass(mContext, BikeInUseRedActivity.class);
                                        }
                                        startActivity(intent);
                                        finish();
                                    } else if (requestCode == Constants.REQUEST_SCAN_INUSE) {
                                        finish();
                                    }
                                }
                            } else {
                                Log.i("params empty", "bikeLicenseDesignated" + bikeLicenseDesignated + "     " + "bikeid" + bikeId + "     " + "lockType" + lockType + "    " + "power" + power + "   " + "latitude" + curPosition.latitude + "     " + "lockStatus" + lockStatus
                                        + "     " + "operationStatus" + operationStatus + "    " + "orderId" + orderId + "     " + "operationType" + operationType);
                                Log.i("params rmpty", MyApplication.getInstance().getRequestFieldMap().toString());

                                ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                            }
                        } else {
                            progressBar_input.setVisibility(View.GONE);
                            ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<OperateBean> call, Throwable t) {
                        progressBar_input.setVisibility(View.GONE);
                        ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                    }
                });
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);

        if (isLightOn) {
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);// 关闭
            camera.setParameters(parameters);
            camera.stopPreview();
            camera.release();
            ivInputUseLight.setImageDrawable(getResources().getDrawable(R.drawable.light_off));
            tvInputUseLight.setTextColor(getResources().getColor(R.color.white));
            isLightOn = false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ClientManager.getClient().unregisterBluetoothStateListener(mBluetoothStateListener);

        if (timerQuery != null) {
            timerQuery.cancel();
            timerQuery = null;
        }
    }

    private void sendHeat() {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL_PHP).sendHeart(MyApplication.getInstance().getRequestFieldMap(), orderId, bikeId, curPosition.longitude + "", curPosition.latitude + "", lockType + "", "0").enqueue(new Callback<HeartBean>() {
            @Override
            public void onResponse(Call<HeartBean> call, Response<HeartBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getErrorCode() == 0) {
                        Log.i("heart", "heart");
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMsg(), Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<HeartBean> call, Throwable t) {
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
            }
        });
    }

    //bike-APP端操作日志记录 操作前
    private void commitBikeLogPre() {
        NetUtils.commitBikeLog(InputSNActivity.this, aMapLocation, bikeId, scanedBikeLicense, (lockType == -1) ? "" : lockType + "", orderId, "", "2", "1", "0", "", "");
    }

    //bike-APP端操作日志记录 操作成功
    private void commitBikeLogSuccess() {
        NetUtils.commitBikeLog(InputSNActivity.this, aMapLocation, bikeId, scanedBikeLicense, (lockType == -1) ? "" : lockType + "", orderId, "", "2", "2", "0", "200", "");
    }

    //bike-APP端操作日志记录 操失败
    private void commitBikeLogFailed(String errorcode, String errormsg) {
        NetUtils.commitBikeLog(InputSNActivity.this, aMapLocation, bikeId, scanedBikeLicense, (lockType == -1) ? "" : lockType + "", orderId, "", "2", "3", "1", errorcode, errormsg);
    }

    /**
     * 蓝牙指令响应回调
     */
    private BluetoothHelper.BluetoothCommandCallback commandCallback = new BluetoothHelper.BluetoothCommandCallback() {

        @Override
        public void onSetReturnMode(boolean isSuccessful) {

        }

        @Override
        public void onGetToken(byte[] btToken) {
            MyApplication.btToken = btToken;
            if (caozuo == 0) {
                BluetoothHelper.getInstance().openLock();
            }
        }

        @Override
        public void onOpenLock(boolean isSuccessful) {
            //开锁成功
            if (isSuccessful) {
                progressBar_input.setVisibility(View.GONE);
                sendBTLockInfo(1, 1);
                commitBikeLogSuccess();
            } else {
                sendBTLockInfo(0, 0);
                progressBar_input.setVisibility(View.GONE);
                ClientManager.getClient().disconnect(btMac);
                final KuaituDialog kuaituDialog = new KuaituDialog(InputSNActivity.this);
                kuaituDialog.setTitle(getString(R.string.dialogTitle));
                kuaituDialog.setMessage("开锁失败，请联系客服");
                kuaituDialog.setYesOnclickListener("确定", new KuaituDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        kuaituDialog.dismiss();

                    }
                });
                kuaituDialog.setNoOnclickListener("取消", new KuaituDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        kuaituDialog.dismiss();

                    }
                });
                kuaituDialog.setCancelable(false);
                kuaituDialog.show();
                commitBikeLogFailed("", "蓝牙开锁失败");
            }
        }

        @Override
        public void onCloseLock(boolean isSuccessful) {

        }

        @Override
        public void onResetLock(boolean isSuccessful) {

        }

        @Override
        public void onGetLockPower(byte power) {

        }

        @Override
        public void onCheckLockStatus(boolean isOpen) {

        }
    };
}
